package com.chatify.app;



import java.util.ArrayList;
import java.util.List;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class ChatActivity extends Activity {
  
    private static String mRecipient;
    private EditText mSendText;
    
    public XMPPConnection xConn;
    public static List<ChatItem> chatit=new ArrayList<ChatItem>();;
    public ChatItem chItem;
    public Handler mHandler = new Handler();
    /**
     * Called with the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Log.i("ChatActivity", "onCreate called");
        setContentView(R.layout.activity_chat);
        xConn=NetworkConnection.xmppConnection;
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
        	 mRecipient = extras.getString("Recipient");
            // and get whatever type user account id is
        }
      //  TextView tvBuddyName=(TextView) this.findViewById(R.id.receipentName);
        //tvBuddyName.setText(mRecipient);
        
        Log.i("ChatActivity", "mRecipient = " + mRecipient);
        
        mSendText = (EditText) this.findViewById(R.id.sendText);
        Log.i("ChatActivity", "mSendText = " + mSendText);
        
        
        //mList = (ListView) this.findViewById(R.id.listMessages);
       // Log.i("ChatActivity", "mList = " + mList);
        setConnection();
        setChatListAdapter();
        
        //chatitem=new List<ChatItem>();
        // Set a listener to send a chat text message
        Button send = (Button) this.findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	SendMessage(mRecipient);
            }
        });
    
    }

    public void SendMessage(String mRecipient){
    	
        String text = mSendText.getText().toString();
        Log.i("ChatActivity", "Sending text [" + text + "] to [" + mRecipient + "]");
        Message msg = new Message(mRecipient, Message.Type.chat);
        msg.setBody(text);
        xConn.sendPacket(msg);
        ChatItem  chat=new ChatItem();
        chat.sendermessage=text;
        chat.Receiver=mRecipient;
        chatit.add(chat); 
        setChatListAdapter();
        mSendText.setText("");
    }
    
    /**
     * Called by Settings dialog when a connection is establised with the XMPP server
     *
     * @param connection
     */
    public void setConnection() {
    	
        if (xConn != null) {
            // Add a packet listener to get messages sent to us
            PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
            xConn.addPacketListener(new PacketListener() {
                public void processPacket(Packet packet) {
                    Message message = (Message) packet;
                    if (message.getBody() != null) {
                        String fromName = StringUtils.parseBareAddress(message.getFrom());
                        Log.i("ChatActivity", "Got text [" + message.getBody() + "] from [" + fromName + "]");
                        
                        ChatItem chItem =new ChatItem();
                        chItem.Receiver=fromName;
                        chItem.receivermessage=message.getBody();
                        chatit.add(chItem);
                        
                        //messages.add(fromName + ":");
                        //messages.add(message.getBody());
                        // Add the incoming message to the list view
                        mHandler.post(new Runnable() {
                            public void run() {
                            	setChatListAdapter();
                            }
                        });
                    }
                }
            }, filter);
        }
    	else{
    		
    	}
    }


    private void setChatListAdapter() {
    	if(chatit!=null){
    		
    	ChatListAdapter adapter =new ChatListAdapter(ChatActivity.this, chatit);
    	ListView mList =(ListView) findViewById(R.id.gossipslistItems);
    	mList.setAdapter(adapter);
    	mList.setOnItemClickListener(new OnItemClickListener()
      	{
      	public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
      	{
      		
         	
      	}
      	});
    	}
     }
}
